package day11;

/*
 * Author: JacksonCoder
 * FileName: AlternatePrint
 * Version: 1.0.0
 * Date: 2025/4/21
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AlternatePrint {

    private static final Object lock = new Object();
    private static boolean numberTurn = true; // 控制轮到谁打印

    public static void main(String[] args) {
        char[] numbers = "12345".toCharArray();
        char[] letters = "ABCDE".toCharArray();

        Thread t1 = new Thread(() -> {
            for (char num : numbers) {
                synchronized (lock) {
                    while (!numberTurn) {
                        try { lock.wait(); } catch (InterruptedException ignored) {}
                    }
                    System.out.print(num);
                    numberTurn = false;
                    lock.notify();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (char letter : letters) {
                synchronized (lock) {
                    while (numberTurn) {
                        try { lock.wait(); } catch (InterruptedException ignored) {}
                    }
                    System.out.print(letter);
                    numberTurn = true;
                    lock.notify();
                }
            }
        });

        t1.start();
        t2.start();
    }

}
